package com.lagou.rpc.consumer.controller;

import com.lagou.rpc.api.IUserService;
import com.lagou.rpc.consumer.proxy.RpcClientProxy;
import com.lagou.rpc.pojo.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description: TODO
 * @Author 50705 2021/11/6
 **/

@RestController
@RequestMapping(value = "/rpc")
public class RPCController {


    private int server;

    // 服务1的连接信息
    String server1Ip = "127.0.0.1";
    int server1Port = 8899;

    // 服务2的连接信息
    String server2Ip = "127.0.0.1";
    int server2Port = 8999;

    @GetMapping("/getUser")
    public User getUserById(int id){

        server++;

        // 判断使用哪个服务
        String serverIp;
        int serverPort;
        if(server%2 == 0){
            serverIp = server1Ip;
            serverPort = server1Port;
        }else {
            serverIp = server2Ip;
            serverPort = server2Port;
        }

        IUserService userService = (IUserService) RpcClientProxy.createProxy(IUserService.class, serverIp, serverPort);
        User user = userService.getById(id);

        return user;
    }
}
